#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Wave Relay
# Copyright (C) 2010 Yakovlev Vladimir [nvbn.rm@gmail.com]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4 import QtCore, QtGui
import commands
from search import LastSearch
from vk import Vkontakte
import ConfigParser
import pylast

class config:
    """Class with config values"""
    random = False
    repeat = True
    repeat_song = False
    vk_login = None
    vk_password = None
    ls_login = None
    ls_password = None
    ls_scrob = True
    save_playlist = True
    save_search = True

    def __init__(self, parent):
        """Initiale config class

        Keyword arguments:
        parent -- Main class

        Returns: None

        """
        self.home = commands.getoutput("echo $HOME")
        self.playlist = '%s/.lplayer/playlist.json' % (self.home, )
        self.search = '%s/.lplayer/search.json' % (self.home, )
        config = ConfigParser.RawConfigParser()
        self.parent = parent
        try:
            self.read_config(config)
        except:
            self.create_default_config(config)

    def create_default_config(self, config):
        """Create default config file

        Keyword arguments:
        config -- ConfigParser class

        Returns: Int

        """
        try:
            config.add_section('accounts')
            config.set('accounts', 'vk_login', self.vk_login)
            config.set('accounts', 'vk_password', self.vk_password)
            config.set('accounts', 'ls_login', self.ls_login)
            config.set('accounts', 'ls_password', self.ls_password)
            config.set('accounts', 'ls_scrob', self.ls_scrob)
            config.add_section('player')
            config.set('player', 'random', self.random)
            config.set('player', 'repeat', self.repeat)
            config.set('player', 'repeat_song', self.repeat_song)
            config.set('player', 'save_playlist', self.save_playlist)
            config.set('player', 'save_search', self.save_search)
            with open('%s/.lplayer/config' % (self.home, ), 'w') as configfile:
                config.write(configfile)
        except:
            return(0)

    def read_config(self, config):
        """Read config file

        Keyword arguments:
        config -- ConfigParser class

        Returns: Int

        """
        try:
            config.read("%s/.lplayer/config" % (self.home, ))
        except:
            pass
           # config.read("/etc/lpl")
            #os.system('cp /etc/lpl "$HOME"/.config/')
        self.vk_login = config.get('accounts', 'vk_login')
        self.vk_password = config.get('accounts', 'vk_password')
        self.ls_login = config.get('accounts', 'ls_login')
        self.ls_password = config.get('accounts', 'ls_password')
        self.ls_scrob = config.get('accounts', 'ls_scrob')
        self.random = config.get('player', 'random')
        self.repeat = config.get('player', 'repeat')
        self.repeat_song = config.get('player', 'repeat_song')
        self.save_playlist = config.get('player', 'save_playlist')
        self.save_playlist = config.get('player', 'save_search')
        return(0)

    @QtCore.pyqtSlot()
    def show_settings(self):
        """Show settings dialog"""
        self.settings = self.parent.settings_widget
        self.dialog = QtGui.QDialog()
        self.settings.setupUi(self.dialog)
        QtCore.QObject.connect(self.settings.ok, QtCore.SIGNAL('clicked()'), self.save)
        QtCore.QObject.connect(self.settings.cancel, QtCore.SIGNAL('clicked()'), self.cancel)
        self.settings.vk_login.setText(str(self.vk_login))
        self.settings.vk_password.setText(str(self.vk_password))
        self.settings.ls_login.setText(str(self.ls_login))
        self.settings.ls_password.setText(str(self.ls_password))
        if self.ls_scrob:
            self.settings.ls_scrob.setChecked(True)
        self.dialog.setModal(True)
        self.dialog.exec_()

    def save_settings(self):
        """Save settings to config file"""
        config = ConfigParser.RawConfigParser()
        self.create_default_config(config)

    @QtCore.pyqtSlot()
    def save(self):
        """Save values from config dialog"""
        self.vk_login = str(self.settings.vk_login.text())
        self.vk_password = str(self.settings.vk_password.text())
        self.ls_login = str(self.settings.ls_login.text())
        self.ls_password = str(self.settings.ls_password.text())
        self.ls_scrob = str(self.settings.ls_scrob.isChecked())
        self.save_settings()
        self.parent.vk = Vkontakte(self.vk_login, self.vk_password)
        try:
            self.parent.lastfm = LastSearch(self.ls_login, self.ls_password)
            self.parent.scrobbler = self.parent.lastfm.network.get_scrobbler('tst', '1.0')
        except pylast.WSError:
            self.parent.lastfm = LastSearch()
            self.ls_scrob = False
            self.parent.scrobbler = None
        self.dialog.done(1)

    @QtCore.pyqtSlot()
    def cancel(self):
        """Cancel config filling"""
        self.dialog.done(1)

    @QtCore.pyqtSlot()
    def set_random(self):
        """Enable random playing ordering"""
        self.parent.widget.actionLinear.setChecked(False)
        self.parent.widget.actionShuffle.setChecked(True)
        self.random = True
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_linear(self):
        """Enable linear playing ordering"""
        self.parent.widget.actionLinear.setChecked(True)
        self.parent.widget.actionShuffle.setChecked(False)
        self.random = False
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_repeat(self):
        """Enable playlist repeatment"""
        self.parent.widget.actionNone.setChecked(False)
        self.parent.widget.actionTrack.setChecked(False)
        self.parent.widget.actionAll.setChecked(True)
        self.repeat = True
        self.repeat_song = False
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_no_repeat(self):
        """Disable playlist repeatment"""
        self.parent.widget.actionNone.setChecked(True)
        self.parent.widget.actionTrack.setChecked(False)
        self.parent.widget.actionAll.setChecked(False)
        self.repeat = False
        self.repeat_song = False
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_repeat_song(self):
        """Enable one song repeatment"""
        self.parent.widget.actionNone.setChecked(False)
        self.parent.widget.actionTrack.setChecked(True)
        self.parent.widget.actionAll.setChecked(False)
        self.repeat = False
        self.repeat_song = True
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_save_search(self):
        """Enable save search list on exit"""
        state = self.parent.widget.actionRestore_search.isChecked()
        self.save_search = state
        self.save_settings()

    @QtCore.pyqtSlot()
    def set_save_playlist(self):
        """Enable save playlist on exit"""
        state = self.parent.widget.actionRestore_playlist.isChecked()
        self.save_playlist = state
        self.save_settings()


